package com.module.controller;

import com.module.framework.domain.Public;
import com.module.framework.domain.ResponseResult;
import com.module.framework.dto.BookListDTO;
import com.module.framework.dto.PbulicDTO;
import com.module.framework.service.PublicService;
import com.module.framework.vo.PageVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/public")
public class PublicController {

    @Resource
    private PublicService publicService;

    @PostMapping("/page")
    public ResponseResult page(@RequestBody PbulicDTO publicDTO) {
        PageVO pageVO = publicService.publicList(publicDTO);
        return ResponseResult.okResult(pageVO);
    }

    @GetMapping("/list")
    public ResponseResult getList() {
        List<Public> publics = publicService.getList();
        return ResponseResult.okResult(publics);
    }

    @PostMapping("/add")
    public ResponseResult add(@RequestBody Public publics) {
        publics.setCreateTime(new java.sql.Date(System.currentTimeMillis()));
        publicService.save(publics);
        return ResponseResult.okResult();
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody Public publics) {
        publics.setCreateTime(new java.sql.Date(System.currentTimeMillis()));
        publicService.updateById(publics);
        return ResponseResult.okResult();
    }

    @GetMapping("/get")
    public ResponseResult get(Integer id) {
        Public publics = publicService.getById(id);
        return ResponseResult.okResult(publics);
    }

    @GetMapping("/delete")
    public ResponseResult delete(Integer id) {
        System.out.println("Received ID in controller: " + id);
        publicService.removeById(id);
        return ResponseResult.okResult();
    }


}
